// pages/message/index.js
var app = getApp(), common_Js = require("../../component/common.js");
import Toast from '../../component/vant/toast/toast';
var  fetchApi = require("../../utils/httpPost.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageSize:10,
    pageIndex: 1,
    recordList:[],
    hasData:false,
    loading: false,
    netWork: false,
    isLoading: true,
    currentTab:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  onChange:function(event) {
    console.log('onChange',event)
    var index = event.detail.index;
    this.data.currentTab = index;
    this.data.pageIndex = 1;
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      recordList:[],
      currentTab: index
    });
    if(index == 0){
      this.onHttpInitEdge()
    }else{
      this.onHttpInitSmoke()
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.onHttpInitEdge();
  },

  /**
   * 获取页面数据的 http请求
   */
  onHttpInitEdge: function () {
    var that = this
    fetchApi.httpPost('/edge/record/list', {
      pageIndex:that.data.pageIndex,
      pageSize:that.data.pageSize
    }, function (res) {
      console.log("上报记录响应数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1;
          var records = res.data.data.records;
          console.log('records',records)
          for (var index in records) {
            that.data.recordList.splice(that.data.recordList.length, 0, records[index]);
          }
          
          that.setData({
            hasData: true,
            recordList: that.data.recordList
          })
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInitSmoke: function () {
    var that = this
    fetchApi.httpPost('/device/smoke/record/list', {
      pageIndex:that.data.pageIndex,
      pageSize:that.data.pageSize
    }, function (res) {
      console.log("上报记录响应数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1;
          var records = res.data.data;
          console.log('records',records)
          for (var index in records) {
            that.data.recordList.splice(that.data.recordList.length, 0, records[index]);
          }
          
          that.setData({
            hasData: true,
            recordList: that.data.recordList
          })
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  showPic(e){
    console.log()
    var item = e.currentTarget.dataset.item;
    console.log('showPic', item)
    var c = [];
    c.push(item.picUrl)
    common_Js.preImg(0, c)    
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      recordList:[]
    })
    this.data.pageIndex = 1
    
    if(this.data.currentTab == 0){
      this.onHttpInitEdge()
    }else{
      this.onHttpInitSmoke()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // this.setData({
    //   isLoading: true
    // })
    if(this.data.currentTab == 0){
      this.onHttpInitEdge()
    }else{
      this.onHttpInitSmoke()
    }
  },

})